//
//  SettingViewController.m
//  didacar
//
//  Created by Yang Yuexia on 15/12/8.
//  Copyright © 2015年 dzmmac. All rights reserved.
//

#import "SettingViewController.h"
#import "SettingCell.h"
#import "UserInfoManager.h"
#import "SVProgressHUD.h"
#import "UIButton+Dzm.h"
#import "H5BaseViewController.h"

@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UIAlertViewDelegate>

@property (strong, nonatomic) NSArray *dataList;

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) IBOutlet UIView *footerView;

@property (weak, nonatomic) IBOutlet UIButton *quitButton;

@property (weak, nonatomic) IBOutlet UILabel *versionLabel;

- (IBAction)quitAction:(id)sender;

@end

@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = @"设置";
    self.dataList = @[@"常见问题",@"用户指南",@"购票、退票规则",@"使用协议",@"关于我们"];
    
    [self setLeftBtnItem];
    [self.quitButton styleButton];
    
    
    self.versionLabel.text = [NSString stringWithFormat:@"v%@",[[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleShortVersionString"]];
    self.tableView.tableFooterView = self.footerView;
    
    
    if (USERINFOManager.getUserInfo.loginStatus) {
        self.quitButton.hidden = NO;
    }else{
        self.quitButton.hidden = YES;
    }
}


#pragma mark ---- 列表代理 ----
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SettingCell *cell = [tableView dequeueReusableCellWithIdentifier:@"SettingCell"];
    cell.title.text = self.dataList[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    H5BaseViewController *h5Base = [[H5BaseViewController alloc ] init];
    switch (indexPath.row) {
        // 常见问题
        case 0:
        {
            h5Base.webViewUrl = QUESTION;
        }
            break;
        // 用户指南
        case 1:
        {
            h5Base.webViewUrl = USERGUIDE;
        }
            break;
            // 购票、退票规则
        case 2:
        {
           h5Base.webViewUrl = BUYRETURNTICKET;
        }
            break;
            // 使用协议
        case 3:
        {
            h5Base.webViewUrl = USERRULE;
        }
            break;
            // 关于我们
        case 4:
        {

            h5Base.webViewUrl = ABOUTUS;
          
        }
            break;
        default:
            break;
    }
      [self.navigationController pushViewController:h5Base animated:YES];
    
    
}

#pragma mark ---- 退出登陆 ----
- (IBAction)quitAction:(id)sender
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"您确定要退出账户吗？" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    [alert show];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1) {
        [USERINFOManager updateLoginStatus:NO];
        [[NSNotificationCenter defaultCenter] postNotificationName:LogoutSuccess object:nil];
        [self.navigationController popViewControllerAnimated:YES];
        [SVProgressHUD showSuccessWithStatus:@"已退出当前账户"];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
